package movee.manager.ftp;

import movee.domain.constants.FileSyncErrType;
import movee.utils.DelayUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;


/**
 * ftp便利工具
 *
 * @author movee
 */
@Service
@Slf4j
public class FtpTools {

    public FileSyncErrType downloadFile(String host, int port, String userName, String password,
                                        String remotePath, String localPath) {

        FtpClient ftpClient = null;
        try {
            ftpClient = new FtpClient(host, port, userName, password);
            return ftpClient.downLoad(remotePath, localPath);
        } catch (Exception e) {
            log.warn("downloadFile failed. host: {}, remotePath: {}, localPath: {}, exception stack: {}",
                    host, remotePath, localPath, ExceptionUtils.getStackTrace(e));
            return FileSyncErrType.DOWNLOAD_EXCEPT;
        } finally {
            if (ftpClient != null) {
                try {
                    ftpClient.close();
                } catch (IOException e) {
                    log.error("close ftpClient failed. host: {}, remotePath: {}, localPath: {}, exception stack: {}",
                            host, remotePath, localPath, ExceptionUtils.getStackTrace(e));
                }
            }
        }
    }

    public FileSyncErrType downloadFileWithRetry(String host, int port, String userName, String password,
                              String remotePath, String localPath) {

        FileSyncErrType result = downloadFile(host, port, userName, password, remotePath, localPath);
        if (result != FileSyncErrType.OK
                && result != FileSyncErrType.FILE_NOT_EXIST
                && result != FileSyncErrType.FILE_LEN_ZERO) {
            DelayUtils.delayBySleep(10L);
            result = downloadFile(host, port, userName, password, remotePath, localPath);
        }

        return result;
    }

    /**
     * 递归删除ftp server上的目录以及子目录
     * @param host host
     * @param port port
     * @param userName userName
     * @param password password
     * @param remotePath remotePath
     * @return true or false
     */
    public boolean deleteDirectory(String host, int port, String userName, String password, String remotePath) {
        FtpClient ftpClient = null;
        try {
            ftpClient = new FtpClient(host, port, userName, password);
            return ftpClient.deleteDirectory(remotePath);
        } catch (Exception e) {
            log.error("deleteDirectory failed. host: {}, remotePath: {}, exception stack: {}",
                    host, remotePath, ExceptionUtils.getStackTrace(e));
            return false;
        } finally {
            if (ftpClient != null) {
                try {
                    ftpClient.close();
                } catch (IOException e) {
                    log.error("close ftpClient failed. host: {}, remotePath: {},  exception stack: {}",
                            host, remotePath, ExceptionUtils.getStackTrace(e));
                }
            }
        }
    }
}
